package com.smart.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

import com.smart.model.PCAuthority;
import com.smart.util.DBUtil;
import com.smart.util.SAXParserHandler;


/**
 * Servlet implementation class InitialServlet
 */
@WebServlet("/InitialServlet")
public class InitialServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public InitialServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
		System.out.println("InitialServlet init method");
		ServletContext sc = config.getServletContext();
		String realPath = sc.getRealPath("/");
		sc.setAttribute("realPath",realPath);
		//读取上传文件的路径
		Properties props = new Properties();
		String path = realPath+"ini\\upload.ini";
		File f = new File(path);
		if(f.exists())
		{
		 System.out.println("file exists");
		 try {
			FileInputStream fis = new FileInputStream(f);
			props.load(fis);
			String imagePath = props.getProperty("imagePath");
			sc.setAttribute("imagePath",imagePath);
		 } catch (FileNotFoundException e) {
			e.printStackTrace();
		 } catch (IOException e) {
			e.printStackTrace();
		 }
		}else
		{
			System.out.println("upload.ini file not exists");
		}
		
		System.out.println("init PCAuthority start.....");
		//下面解析权限配置的文件，然后放入servletContext中
		//获取一个SAXParserFactory的实例
		SAXParserFactory factory=SAXParserFactory.newInstance();
		//通过SAXParserFactory获取SAXParser的实例
		try {
				SAXParser parser=factory.newSAXParser();
				SAXParserHandler handler=new SAXParserHandler();
				parser.parse(realPath+"WEB-INF/configs/PCAuthority.xml", handler);
					List<PCAuthority> pcAuthorityList=handler.getPcAuthorityList();
					for(PCAuthority pcAuthority:pcAuthorityList)
					{
						String url=pcAuthority.getUrl();
						List<String> role=pcAuthority.getRole();
					    sc.setAttribute(url,role);
					}
				} catch (ParserConfigurationException e) {
					e.printStackTrace();
				} catch (SAXException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
		System.out.println("init PCAuthority finish.....");
	}

	@Override
	public void destroy() {
		super.destroy();
		System.out.println("进行关闭数据库连接操作");
		Connection conn=DBUtil.getConnection();
		if(conn!=null)
		{
			try 
			{
				conn.close();
				System.out.println("数据库连接关闭成功");
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		else
		{
			System.out.println("数据库连接为空，不需要关闭");
		}
	}
	
}
